package com.rockcent.job.task;

import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.ProductEvent;
import com.rockcent.mall.lib.domain.ProductStatistics;
import com.rockcent.mall.lib.domain.enumclass.ProductEventType;
import com.rockcent.mall.lib.repository.MerchantProductStatisticsRepository;
import com.rockcent.mall.lib.repository.ProductEventRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by DK on 17/1/5.
 * 产品统计视图
 */
@Component
public class ProductStatisticsTask {

    final Logger log = LoggerFactory.getLogger(ProductStatisticsTask.class);

    @Autowired
    private MerchantProductStatisticsRepository merchantProductStatisticsRepository;

    @Autowired
    private ProductEventRepository productEventRepository;

    /**
     *  一个小时跑一次
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    @Transactional
    public void synchronizeProductStatistics() {
        List<ProductEvent> productEventList = productEventRepository.findTwoHourData();
//        log.info("======================>产品统计视图定时任务执行start============================一个小时跑一次");
        productEventList.stream().forEach(productEvent -> {
            if (productEvent.getStatisticsTask() == Whether.NO) {
                ProductStatistics statistics = merchantProductStatisticsRepository.findByProductId(productEvent.getProductId());
                if (statistics != null) {
                    //销售数
                    if (ProductEventType.SALED == productEvent.getEventType()) {
                        statistics.setSaleCount(statistics.getSaleCount() + productEvent.getNum());
                    }
                    //分享数
                    if (ProductEventType.SHARED == productEvent.getEventType()) {
                        statistics.setShareCount(statistics.getShareCount() + 1);
                    }
                    //核销数
                    if (ProductEventType.VERIFIED == productEvent.getEventType()) {
                        statistics.setVerifyCount(statistics.getVerifyCount() + productEvent.getNum());
                    }
                    //浏览数
                    if (ProductEventType.VIEWED == productEvent.getEventType()) {
                        statistics.setViewCount(statistics.getViewCount() + 1);
                    }
                    //点击数
                    if (ProductEventType.CLICKED == productEvent.getEventType()) {
                        statistics.setClickCount(statistics.getClickCount() + 1);
                    }
                    //下单数
                    if (ProductEventType.ORDERED == productEvent.getEventType()) {
                        statistics.setOrderCount(statistics.getOrderCount() + productEvent.getNum());
                    }
                    merchantProductStatisticsRepository.save(statistics);
                }
            }
            productEvent.setStatisticsTask(Whether.YES);
            productEventRepository.save(productEvent);

        });
//        log.info("======================>产品统计视图定时任务执行end============================一个小时跑一次");
    }
}
